package io.renren.modules.sys.controller;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.renren.common.dto.SongSetSong;
import io.renren.common.dto.TeachSong;
import io.renren.modules.sys.entity.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.service.TeachService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author wangyi
 * @email 794651397@qq.com
 * @date 2018-02-24 10:46:44
 */
@RestController
@RequestMapping("/sys/teach")
public class TeachController {
	@Autowired
	private TeachService teachService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TeachEntity> teachList = teachService.queryList(query);
		int total = teachService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(teachList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		TeachEntity teach = teachService.queryObject(id);
		
		return R.ok().put("teach", teach);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TeachEntity teach){
        teach.setCreateTime(new Date());
		teachService.save(teach);
		
		return R.ok().put("teachId",teach.getId());
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TeachEntity teach){
	    teach.setUpdateTime(new Date());
		teachService.update(teach);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids){
		teachService.deleteBatch(ids);
		
		return R.ok();
	}

    /**
     * 信息
     */
    @RequestMapping("/getSong/{id}")
    public R getSong(@PathVariable("id") Long id){
        List<SongEntity> list=teachService.getSongByTeachId(id);
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/getVedio/{id}")
    public R getVedio(@PathVariable("id") Long id){
        List<VedioEntity> list=teachService.getVedioByTeachId(id);
        return R.ok().put("list", list);
    }

    /**
     * 关联曲谱
     */
    @RequestMapping("/teachSong")
    public R songSetSong(@RequestBody TeachSong teachSong){
        Integer teachId=teachSong.getTeachId();
        Integer[] songIds=teachSong.getSongIds();
        teachService.deleteTeachSong(teachId);
        List<TeachSongEntity> l=new LinkedList<>();
        for (Integer songId : songIds) {
            TeachSongEntity teachSongEntity=new TeachSongEntity();
            teachSongEntity.setSongId(songId.longValue());
            teachSongEntity.setTeachId(teachId.longValue());
            teachSongEntity.setCreateTime(new Date());
            l.add(teachSongEntity);
        }
        teachService.addTeachSongBatch(l);
        return R.ok();
    }

    /**
     * 关联视频
     */
    @RequestMapping("/teachVedio")
    public R teachVideo(@RequestBody List<VedioEntity> vedioEntityList){
        Integer teachId=0;
        for (VedioEntity vedioEntity : vedioEntityList) {
            vedioEntity.setCreateTime(new Date());
            teachId=vedioEntity.getTeachId();
        }
        teachService.deleteTeachVideo(teachId);
        if(vedioEntityList.size()>0){
            teachService.addTeachVedioBatch(vedioEntityList);
        }
        return R.ok();
    }

    /**
     * 关联视频
     */
    @RequestMapping("/teachVedioDel")
    public R teachVedioDel(@RequestParam Integer teachId){
        teachService.deleteTeachVideo(teachId);
        return R.ok();
    }

}
